package com.lizhijun.controller;


import com.lizhijun.pojo.Employee;
import com.lizhijun.service.EmployeeService;
import com.lizhijun.utils.LayuiTransform;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/pTable")
    @ResponseBody
    public LayuiTransform<Employee> findAll(){
        List<Employee> employeeList = employeeService.findAbsence();
        LayuiTransform<Employee> layuiTransform = new LayuiTransform<>();
        layuiTransform.setData(employeeList);
        layuiTransform.setCode(0);
        layuiTransform.setCount(employeeList.size());
        layuiTransform.setMsg("success");
        return layuiTransform;
    }

    @RequestMapping("/update")
    @ResponseBody
    public void update(HttpServletRequest request){
        String eid = request.getParameter("eid");
        String s = request.getParameter("absence");
        Integer absence = Integer.parseInt(s);
//        System.out.println(eid);
//        System.out.println(absence);
        Employee employee = new Employee();
        employee.setEid(eid);
        employee.setAbsence(absence);
        employeeService.update(employee);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public void delete(HttpServletRequest request){
        String eid = request.getParameter("eid");
        Employee employee = new Employee();
        employee.setEid(eid);
        employeeService.delete(employee);
    }

    @RequestMapping("/pTable2")
    @ResponseBody
    public LayuiTransform<Employee> findBonus(){
        List<Employee> employeeList = employeeService.findBonus();
        LayuiTransform<Employee> layuiTransform = new LayuiTransform<>();
        layuiTransform.setData(employeeList);
        layuiTransform.setCode(0);
        layuiTransform.setCount(employeeList.size());
        layuiTransform.setMsg("success");
        return layuiTransform;
    }

    @RequestMapping("/updateBonus")
    @ResponseBody
    public void updateBonus(HttpServletRequest request){
        String eid = request.getParameter("eid");
        String s = request.getParameter("bonus");
        Integer bonus = Integer.parseInt(s);
        Employee employee = new Employee();
        employee.setEid(eid);
        employee.setBonus(bonus);
        employeeService.updateBonus(employee);
    }

    @RequestMapping("/updateDeduction")
    @ResponseBody
    public void updateDeduction(HttpServletRequest request){
        String eid = request.getParameter("eid");
        String s = request.getParameter("deduction");
        Integer deduction = Integer.parseInt(s);
        Employee employee = new Employee();
        employee.setEid(eid);
        employee.setDeduction(deduction);
        employeeService.updateDeduction(employee);
    }

    @RequestMapping("/pTable4")
    @ResponseBody
    public LayuiTransform<Employee> findDepartment(){
        List<Employee> employeeList = employeeService.findDepartment();
        LayuiTransform<Employee> layuiTransform = new LayuiTransform<>();
        layuiTransform.setData(employeeList);
        layuiTransform.setCode(0);
        layuiTransform.setCount(employeeList.size());
        layuiTransform.setMsg("success");
        return layuiTransform;
    }

    @RequestMapping("/insert")
    @ResponseBody
    public void insertEmployee(HttpServletRequest request){
        String eid= request.getParameter("eid");
        String ename = request.getParameter("ename");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String phone = request.getParameter("phone");
        Employee employee = new Employee();
        employee.setEid(eid);
        employee.setEname(ename);
        employee.setSex(sex);
        employee.setBirthday(birthday);
        employee.setPhone(phone);
        employeeService.insertEmployee(employee);
    }

    @RequestMapping("/findStatus")
    @ResponseBody
    public LayuiTransform<Employee> findStatus(){
        List<Employee> employeeList = employeeService.findStatus();
        LayuiTransform<Employee> layuiTransform = new LayuiTransform<>();
        layuiTransform.setData(employeeList);
        layuiTransform.setCode(0);
        layuiTransform.setCount(employeeList.size());
        layuiTransform.setMsg("success");
        return layuiTransform;
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public void updateStatus(HttpServletRequest request){
        String eid = request.getParameter("eid");
        String status = request.getParameter("status");
        Employee employee = new Employee();
        employee.setEid(eid);
        employee.setStatus(status);
        employeeService.updateStatus(employee);
    }

}
